package com.linguochao.rabbitmq.producer;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class RabbitAdminService {

    @Autowired
    private RabbitAdmin rabbitAdmin;

    @Value("${spring.rabbitmq.batchQueue}")
    private String[] queueList;

    public void declareQueue(String exchange){
        // 声明交换机
        rabbitAdmin.declareExchange(new DirectExchange(exchange));

        Map<String, Object> arguments = new HashMap<>();
        // 单节点活跃
        arguments.put("x-single-active-consumer", true);

        for (String queue : queueList) {
            // 声明队列
            rabbitAdmin.declareQueue(new Queue(queue,true,false,false,arguments));
            // 绑定
            rabbitAdmin.declareBinding(new Binding(queue,Binding.DestinationType.QUEUE,exchange,queue,null));
        }


    }


}
